package com.talent.service.user.controller;

import com.talent.common.model.TalentResult;
import com.talent.model.entity.user.TlUser;
import com.talent.model.vo.LoginVo;
import com.talent.model.wx.WxOpenIdResultVo;
import com.talent.model.wx.WxPhoneResultVo;
import com.talent.model.wx.dto.WxLoginDto;
import com.talent.service.base.wx.WxFeignClient;
import com.talent.service.user.service.ITlUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author watson
 * @Description"
 * @date: 2025/3/13 20:11
 */
@RestController
@RequestMapping("wx")
@RequiredArgsConstructor
public class WxLoginController {


    private final WxFeignClient wxFeignClient;

    private final ITlUserService tlUserService;

    @GetMapping("login")
    public TalentResult<LoginVo> wxLogin(WxLoginDto loginDto){


       //1， 根据code掉微信获取openId
        WxOpenIdResultVo wxOpenIdResultVo = wxFeignClient.getOpenId("", "", loginDto.getCode(), null);

        String openid = wxOpenIdResultVo.getOpenid();
        TlUser tlUser = tlUserService.findUserByOpenId(openid);
        // 判断openId是否为空
        // openId为空 新增user对象 构建数据openId
        if(null == tlUser){
            tlUser = new TlUser();
            tlUser.setOpenId(openid);
        }

        // openId不为空 根据调用微信api后去手机号
        WxPhoneResultVo userPhone = wxFeignClient.getUserPhone("", openid, loginDto.getPhoneCode());
        //保存或修改客户
        tlUser.setPhone(userPhone.getPhone_info().getPhoneNumber());


        //生成token
        return null;
    }



}
